﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class AccountDao : ConectionClass
    {
        public static void AddNew(Account account)
        {

            OleDbConnection conn = CreateConnection();
            string strComm = @"Insert Into Account([Username], [Password], [HoTen], [DiaChi], [NgaySinh], [SoDienThoai], [Email]) Values(?, ?, ?, ?, ?, ?, ?);";
            OleDbCommand comm = new OleDbCommand(strComm, conn);
            comm.Parameters.Add("@Username", OleDbType.VarChar).Value = account.Username;
            comm.Parameters.Add("@Password", OleDbType.VarChar).Value = account.Password;
            comm.Parameters.Add("@HoTen", OleDbType.VarChar).Value = account.HoTen;
            comm.Parameters.Add("@DiaChi", OleDbType.VarChar).Value = account.DiaChi;
            comm.Parameters.Add("@NgaySinh", OleDbType.DBDate).Value = account.NgaySinh;
            comm.Parameters.Add("@SoDienThoai", OleDbType.VarChar).Value = account.SoDienThoai;
            comm.Parameters.Add("@Email", OleDbType.VarChar).Value = account.Email;
          
            try
            {                
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            }

            conn.Close();
        }

        public static void SetPosition(string username, string position)
        {
            OleDbConnection conn = CreateConnection();
            string strComm = "Update [Account] Set [Position] = ? Where [Username] = ?";
            OleDbCommand comm = new OleDbCommand(strComm, conn);
            comm.Parameters.Add("@position", OleDbType.VarChar).Value = position;
            comm.Parameters.Add("@Username", OleDbType.VarChar).Value = username;
            try
            {
                int i = comm.ExecuteNonQuery();
                if (i == 0)
                {
                    conn.Close();
                    throw new Exception("username is not valid");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            }
        }

        public static void SetStatus(string username, string status)
        {
            OleDbConnection conn = CreateConnection();
            string strComm = "Update [Account] Set [Status] = ? Where [Username] = ?";
            OleDbCommand comm = new OleDbCommand(strComm, conn);
            comm.Parameters.Add("@Status", OleDbType.VarChar).Value = status;
            comm.Parameters.Add("@Username", OleDbType.VarChar).Value = username;
            try
            {
                int i = comm.ExecuteNonQuery();
                if (i == 0)
                {
                    conn.Close();
                    throw new Exception("username is not valid");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            }
        }
    }
}
